clear

*generate data for Matlab Bernstein estimations
use data_long,clear
keep if condition==0 
drop cs 
egen cs=group(responseid choiceid) 
reshape wide initial_price discount click chosen rating,i(cs) j(option_id)
gen choice=1*(chosen1==1)+2*(chosen2==1)+3*(chosen3==1)
rename (initial_price1 initial_price2 initial_price3 discount1 discount2 discount3)(x1 x2 x3 z1 z2 z3)
drop cs responseid click* chosen*
drop choiceid condition
export delimited data,replace

*************************************************************************************************
**************************************************Table 2****************************************
*************************************************************************************************
*Table 2, column 1, column 2 standard logit results, and point estimates for coefficient ratio
use data_long,clear
clogit chosen initial_price discount rating if condition==1,group(cs)
gen fullrat=_b[discount]/_b[initial_price] 
clogit chosen initial_price discount rating if condition==0,group(cs)
gen partrat=_b[discount]/_b[initial_price] 
gen r=0
save simtable,replace

*Table 2, bootstrap 1000 times to get the standard errors for coefficient ratio
forvalues i=1/1000{
use data_long,clear
display "Draw is `i'"
set seed 400`i'
bsample, cluster(cs) idcluster(newcs)
drop cs
rename newcs cs
clogit chosen initial_price discount rating if condition==1,group(cs)
gen fullrat=_b[discount]/_b[initial_price] 
clogit chosen initial_price discount rating if condition==0,group(cs)
gen partrat=_b[discount]/_b[initial_price] 
gen r=`i'
keep if _n==1
append using simtable 
save simtable,replace 
}

use simtable,clear
drop if r==0 
sum fullrat partrat 

*Table2 Column 3
*estimates for alpha_price and alpha_rating, using the bottome quartile of z variation choice sets
use data_long,clear
keep if condition==0
bys cs: egen sddiscount = sd(discount)
*limited z variation method using the lowest quartile
xtile sd4 = sddiscount, nq(4)
clogit chosen initial_price discount rating if  sd4 == 1, group(cs)

*beta_discount point estimate comes from ratio point estimate * alpha_price 
*bootstrap for standard errors for beta_discount, based on SOD method
*'ratios' and 'indices_bootstrap' are ratio estimates and choice set used in the Berstein boostraps estimations
clear
import delimited using ratios 
save ratios,replace 

clear 
import delimited using indices_bootstrap 
save indices_bootstrap,replace 

*match indices and choice set ID 
clear 
import delimited using data
gen indices=_n
save data_indices,replace 

*first establish the first loop 
use ratios,clear
keep if _n==1 
tempfile boot
save `boot',replace 

use indices_bootstrap,clear 
keep v1 
rename v1 indices 
merge m:1 indices using data_indices
keep if _merge ==3 
drop _merge 
rename (x1 x2 x3 z1 z2 z3) (initial_price1 initial_price2 initial_price3 discount1 discount2 discount3)
gen chosen1=(choice==1)
gen chosen2=(choice==2)
gen chosen3=(choice==3)
gen cs=_n 
reshape long initial_price discount chosen rating,i(cs) j(option_id)

replace initial_price=-initial_price 
replace discount=-discount
rename initial_price price
append using `boot'
egen v1max=max(v1) 
replace v1=v1max if v1==. 
drop if price==. 

bys cs: egen sddiscount = sd(discount)
*limited z variation method using the lowest quartile
xtile sd4 = sddiscount, nq(4)
clogit chosen price discount rating if  sd4 == 1, group(cs)
gen alpha4=_b[price]
gen beta4=v1*_b[price]

keep alpha* beta* v1 
keep if _n==1 
gen r=1 
tempfile robustbeta
save `robustbeta',replace 

*loop 1000 times 
forvalues i=2/1000{
display "Draw is `i'"
use ratios,clear
keep if _n==`i'
save `boot',replace 

use indices_bootstrap,clear 
keep v`i' 
rename v`i' indices 
merge m:1 indices using data_indices
keep if _merge ==3 
drop _merge 
*drop choiceid 
rename (x1 x2 x3 z1 z2 z3) (initial_price1 initial_price2 initial_price3 discount1 discount2 discount3)
gen chosen1=(choice==1)
gen chosen2=(choice==2)
gen chosen3=(choice==3)
gen cs=_n 
reshape long initial_price discount chosen rating,i(cs) j(option_id)

replace initial_price=-initial_price 
replace discount=-discount
rename initial_price price
append using `boot'
egen v1max=max(v1) 
replace v1=v1max if v1==. 
drop if price==. 

bys cs: egen sddiscount = sd(discount)
*limited z variation method using the lowest quartile
xtile sd4 = sddiscount, nq(4)
clogit chosen price discount rating if  sd4 == 1, group(cs)
gen alpha4=_b[price]
gen beta4=v1*_b[price]



keep alpha* beta* v1 
keep if _n==1 
gen r=`i' 
append using `robustbeta'
save `robustbeta',replace 
}
save robustbeta,replace
sum beta4 



******************welfare analysis on page 29**********************
*welfare using costly information robust method Column 3
use data_long,clear 
replace initial_price=-initial_price 
replace discount=-discount

keep if condition==0 
local f1=-0.230 
local f2=-0.273 
local f3=0.588 
clogit chosen initial_price discount rating if condition==0,group(cs)
local n1=_b[initial_price]
local n2=_b[discount]  
local n3=_b[rating]
gen xbeta0=`n1'*initial_price+`n2'*discount+`n3'*rating 
gen xbeta1=`f1'*initial_price+`f2'*discount+`f3'*rating 

rename cs ID 
gen expxbeta0 = exp(xbeta0)
gen expxbeta1 = exp(xbeta1)
bys ID: egen sumexpxbeta0 = sum(expxbeta0)
bys ID: egen sumexpxbeta1 = sum(expxbeta1)
gen sij0 = expxbeta0/sumexpxbeta0
gen sij1=expxbeta1/sumexpxbeta1
bys ID: egen sumxbeta0 = sum(xbeta0*sij0)
bys ID: egen sumxbeta1 = sum(xbeta1*sij0)
gen DeltaCS = -(1/`f1')*(ln(sumexpxbeta1)-ln(sumexpxbeta0)+sumxbeta0-sumxbeta1)
sum DeltaCS, detail


*welfare using full information condition Column 1
use data_long,clear 
replace initial_price=-initial_price 
replace discount=-discount

clogit chosen initial_price discount rating if condition==1,group(cs)
local f1=_b[initial_price]
local f2=_b[discount] 
local f3=_b[rating]
clogit chosen initial_price discount rating if condition==0,group(cs)
local n1=_b[initial_price]
local n2=_b[discount]  
local n3=_b[rating]

keep if condition==0 
gen xbeta0=`n1'*initial_price+`n2'*discount+`n3'*rating 
gen xbeta1=`f1'*initial_price+`f2'*discount+`f3'*rating 

rename cs ID 
gen expxbeta0 = exp(xbeta0)
gen expxbeta1 = exp(xbeta1)
bys ID: egen sumexpxbeta0 = sum(expxbeta0)
bys ID: egen sumexpxbeta1 = sum(expxbeta1)
gen sij0 = expxbeta0/sumexpxbeta0
gen sij1=expxbeta1/sumexpxbeta1
bys ID: egen sumxbeta0 = sum(xbeta0*sij0)
bys ID: egen sumxbeta1 = sum(xbeta1*sij0)
gen DeltaCS = -(1/`f1')*(ln(sumexpxbeta1)-ln(sumexpxbeta0)+sumxbeta0-sumxbeta1)
sum DeltaCS, detail









